package com.example.capsule_kafka.impl;


import com.example.capsule_kafka.bean.RoomBean;
import com.example.capsule_kafka.interfaces.ChatRecordInterface;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.util.List;

/**
 * @Author: zhuzhiming
 * @Date: 2022/5/16 13:01
 */
@Service
@Slf4j
public class ChatRecordServiceImpl implements ChatRecordInterface {

    @Autowired
    RestHighLevelClient esClient;

    public void createIndex(List<RoomBean> roomBeanListList) {

        roomBeanListList.forEach(roomBean -> {
            GetIndexRequest getIndexRequest = new GetIndexRequest(roomBean.getRoomName());
            try {
                boolean exists = esClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
                if (!exists) {
                    CreateIndexRequest createIndexRequest = new CreateIndexRequest(roomBean.getRoomName().toLowerCase());
                    CreateIndexResponse response = esClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
                    if (response.isAcknowledged()) {
                        log.info("create {} index success", roomBean.getRoomName().toLowerCase());
                    } else {
                        log.info("create {} index fail", roomBean.getRoomName().toLowerCase());
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    @Override
    public void createIndex(String indexName) {

    }

    @Override
    public void createIndex(String indexName, XContentBuilder builder) {

        if (existIndex(indexName)) {
            return;
        }

        CreateIndexResponse response = null;
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        createIndexRequest.mapping(builder);
        try {
            response = esClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        assert response != null;
        if (response.isAcknowledged()) {
            log.info("create {} index success", indexName);
        } else {
            log.error("create {} index success", indexName);
        }

    }

    @Override
    public boolean existIndex(String indexName) {
        boolean exists = false;
        GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
        try {
            exists = esClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return exists;
    }

    @Override
    public void deleteIndex(String indexName) {
        indexName = indexName.toLowerCase();
        if (!existIndex(indexName)) {
            return;
        }
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
        try {
            esClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public RestStatus insert(String indexName, Object object) {
        IndexRequest indexRequest = new IndexRequest(indexName).source(new Gson().toJson(object), XContentType.JSON);
        IndexResponse response = null;
        try {
            response = esClient.index(indexRequest, RequestOptions.DEFAULT);
            return response.status();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public RestStatus update(String indexName, String id, Object object) {
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index(indexName).id(id).doc(XContentType.JSON,"senderName", object);
        try {
            UpdateResponse response = esClient.update(updateRequest, RequestOptions.DEFAULT);
            return response.status();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public RestStatus delete(String indexName, String id) {
        DeleteRequest deleteRequest = new DeleteRequest(indexName);
        deleteRequest.id(id);
        try {
            DeleteResponse response = esClient.delete(deleteRequest, RequestOptions.DEFAULT);
            return response.status();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public SearchResponse search(String indexName, SearchSourceBuilder builder) {
        SearchRequest searchRequest = new SearchRequest(indexName);
        searchRequest.source(builder);
        try {
            SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
            return response;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
